package com.umerclass.ui.home;

import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.umerclass.R;
import com.umerclass.bean.action.HuoDongBean;
import com.umerclass.bean.home.VideoBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.frame.ui.recycleview.PullLoadMoreRecyclerView;
import com.umerclass.frame.ui.recycleview.listener.DataStateListener;
import com.umerclass.frame.ui.recycleview.listener.OptListener;
import com.umerclass.frame.ui.statelayout.StateLayout;
import com.umerclass.ui.action.BaoMIngSureActivity;
import com.umerclass.ui.action.adapter.ActionAdapter;
import com.umerclass.ui.home.adapter.VideoAdapter;
import com.umerclass.utils.ItemClickSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 视频
 */
public class VideoActivity extends BaseActivity implements DataStateListener,
        PullLoadMoreRecyclerView.PullLoadMoreListener, OptListener {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.pullLoadMoreRecyclerView)
    PullLoadMoreRecyclerView pullLoadMoreRecyclerView;
    @BindView(R.id.state_layout)
    StateLayout stateLayout;
    private VideoAdapter videoAdapter;
    private RequestManager glideRequestManager;
    private List<VideoBean> infos = new ArrayList<>();

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_video;
    }

    @Override
    public void init() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.white));
    }

    @Override
    public void loadData() {
        initView();
        todo();
    }

    @OnClick({R.id.linear_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.myTeamGroupList:
                //teamGroupListBeans = (List<TeamGroupListBean>) t.getT();
                //teamAdapter.addDataSource(teamGroupListBeans, t);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.myTeamGroupList:
                swipeRefreshLayout.setRefreshing(false);
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh() {
        //teamAdapter.reset();
        loadData();
    }

    @Override
    public void onLoadMore() {
        loadData();
    }

    @Override
    public void onDataState(int flag) {

        //1:没有网络 2:没有数据 3:获取数据失败 4:显示内容
        switch (flag) {
            case 1:
                stateLayout.showNoNetworkView();
                break;
            case 2:
                stateLayout.showEmptyView();
                break;
            case 3:
                stateLayout.showErrorView();
                break;
            case 4:
                stateLayout.showContentView();
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {

        switch (view.getId()){
            //点击报名
            case R.id.tv_dingyue:
                switchTo(VideoActivity.this, VideoDetailsActivity.class,false);
                break;
            default:break;
        }
    }

    private void initView() {

        glideRequestManager = Glide.with(this);
        stateLayout.setRefreshListener(new StateLayout.OnViewRefreshListener() {
            @Override
            public void refreshClick() {
                loadData();
            }

            @Override
            public void loginClick() {
            }
        });
        pullLoadMoreRecyclerView.init(recyclerView, swipeRefreshLayout);
        videoAdapter = new VideoAdapter(this,glideRequestManager, R.layout.activity_video_item, pullLoadMoreRecyclerView,
                this,this);
        pullLoadMoreRecyclerView.setAdapterTwo(videoAdapter);
        pullLoadMoreRecyclerView.setOnPullLoadMoreListener(this);
        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorAccent, R.color.colorAccent);
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法

        //单击
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView())
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                    }
                });
        //长按
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView()).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
                return false;
            }
        });
    }

    private void todo(){
        VideoBean bean1 = new VideoBean();
        bean1.setPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584350205422&di=4ccce925f48ce9c91852fdc196723465&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2F1%2Fef%2Fcfe31370687.jpg");
        VideoBean bean2 = new VideoBean();
        bean2.setPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584350255231&di=7dcafe77000d89b4d3f110e7e5698e8c&imgtype=0&src=http%3A%2F%2F01.minipic.eastday.com%2F20170510%2F20170510111000_483e4d1cad59039bd8bae6e7900ddce0_6.jpeg");
        infos.add(bean1);
        infos.add(bean2);
        InfoResult infoResult = new InfoResult();
        infoResult.setSuccess(true);
        videoAdapter.addDataSource(infos, infoResult);
    }
}
